﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using YXShop.Common;

namespace YXShop.Web.admin.store
{
    public partial class storemessage_edit : System.Web.UI.Page
    {
        YXShop.BLL.Store.StoreMessage storeMsgBll = new YXShop.BLL.Store.StoreMessage();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
                YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
                if (admin.AdminType == 3)
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("对不起，您没有操作权限");
                }
                if (id > 0)
                {
                    YXShop.Common.PromptInfo.Popedom("014003004", "对不起，您没有权限进行修改");
                    lblOperator.Text = "编辑";
                }
                else
                {
                    YXShop.Common.PromptInfo.Popedom("014003002", "对不起，您没有权限进行新增");
                    lblOperator.Text = "新增";
                }
                InitWebControl();
                if (id > 0)
                {
                    GetMessage(id);
                }
            }
        }

        private void GetMessage(int id)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.Model.Store.StoreMessage s = new Model.Store.StoreMessage();
            YXShop.BLL.Admin.Administrators adminBLL = new BLL.Admin.Administrators();
            YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
            YXShop.BLL.Store.StoreCenter scBLL = new BLL.Store.StoreCenter();
            s = storeMsgBll.GetModelById(id);
            txtTitle.Text = s.MessageTitle;
            txtMessage.Value = s.Message;
            if (admin.AdminType == 2)
            {
                YXShop.Model.Store.StoreCenter sc = scBLL.GetModelById(s.Storecenterid.Value);
                txtStoreCenter.Text = sc.StoreCentername;
                hfStoreCenterid.Value = sc.Sid.Value.ToString();
            }
            hlAccessory1.NavigateUrl = sp.DummyPaht + s.Accessory1;
            hlAccessory1.Text = s.Accessory1.Substring(s.Accessory1.LastIndexOf("/") + 1);
            hlAccessory2.NavigateUrl = sp.DummyPaht + s.Accessory2;
            hlAccessory2.Text = s.Accessory2.Substring(s.Accessory2.LastIndexOf("/") + 1);
            hlAccessory3.NavigateUrl = sp.DummyPaht + s.Accessory3;
            hlAccessory3.Text = s.Accessory3.Substring(s.Accessory3.LastIndexOf("/") + 1);
            hlAccessory4.NavigateUrl = sp.DummyPaht + s.Accessory4;
            hlAccessory4.Text = s.Accessory4.Substring(s.Accessory4.LastIndexOf("/") + 1);
            hlAccessory5.NavigateUrl = sp.DummyPaht + s.Accessory5;
            hlAccessory5.Text = s.Accessory5.Substring(s.Accessory5.LastIndexOf("/") + 1);
            for (int i = 0; i < ddlCatalog.Items.Count; i++)
            {
                if (ddlCatalog.Items[i].Value == s.MessageCategory.ToString())
                {
                    ddlCatalog.Items[i].Selected = true;
                    break;
                }
            }
            lblSenderTime.Text = s.SenderTime.Value.ToString();
            lblSenderID.Text = adminBLL.GetModel(s.SenderId.Value).Name;
            
        }

        #region 验证
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.WebControl.Validate(this.txtTitle, "消息标题不能为空", "isnull", "必填", "该项为必填项");
            this.txtStoreCenter.Attributes.Add("readonly", "readonly");
            this.txtStoreCenter.Attributes.Add("onclick", "selectFile('Storecenterlist',new Array(" + this.hfStoreCenterid.ClientID + "," + this.txtStoreCenter.ClientID + "),310,450,'" + sp.DummyPaht + "');");

            ChangeHope.WebPage.WebControl.Validate(this.txtStoreCenter, "销售中心不能为空", "isnull", "必填", "该项为必填项");
            YXShop.Model.Admin.AdminInfo admin = YXShop.Common.AdministrorManager.Get();
            if (admin.AdminType == 1)
                trStoreCenter.Visible = false;
            else if (admin.AdminType == 2)
                trStoreCenter.Visible = true;
            ddlCatalog.DataSource = ValuePair.GetMessageCategory();
            ddlCatalog.DataTextField = "MessageCategoryName";
            ddlCatalog.DataValueField = "MessageCategoryID";
            ddlCatalog.DataBind();
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        #region 保存数据
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private bool UploadFile(FileUpload fu,HyperLink hl,Panel pTip,HiddenField hf)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.Common.UploadFile upload = new ChangeHope.Common.UploadFile();
            upload.PostedFile = fu;
            upload.ExtensionLim = "";
            upload.FileSaveMethod = "RW";
            upload.FileLengthLim = sp.ImageSize;
            upload.SavePath = "/yxuploadfile/msgaccessory";
            upload.LimitExtension = false;
            bool uploadSucess = upload.Upload();
            if (uploadSucess)
            {
                if (upload.HaveLoad)
                {
                    hl.NavigateUrl=sp.DummyPaht+ upload.FilePath;
                    hf.Value = upload.FilePath;
                    hl.Text = upload.FilePath.Substring(upload.FilePath.LastIndexOf("/") + 1);
                }
            }
            else
            {
                pTip.CssClass = "msgError";
            }

            return uploadSucess;

        }
        private void Save()
        {
            int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");

            YXShop.BLL.Admin.Administrators adminBll = new YXShop.BLL.Admin.Administrators();
            YXShop.Model.Admin.AdminInfo admin=YXShop.Common.AdministrorManager.Get();
            YXShop.Model.Store.StoreMessage message = new YXShop.Model.Store.StoreMessage();


            message.MessageTitle = txtTitle.Text;
            message.MessageCategory = int.Parse(ddlCatalog.SelectedValue);
            message.Message = txtMessage.Value;
            if (admin.AdminType == 1)
            {
                message.MessageType = 0;
                message.Storecenterid = 0;
            }
            else
            {
                message.MessageType = 1;
                message.Storecenterid = int.Parse(hfStoreCenterid.Value);
            }
            message.Accessory1 = hfAccessory1.Value;
            message.Accessory2 = hfAccessory2.Value;
            message.Accessory3 = hfAccessory3.Value;
            message.Accessory4 = hfAccessory4.Value;
            message.Accessory5 = hfAccessory5.Value;
            message.SenderId = admin.AdminId;
            message.SenderTime = DateTime.Now;



            int count = 0;
            if (id > 0)
            {
                message.Id = id;
                count = storeMsgBll.Update(message);
            }
            else
            {
                count = storeMsgBll.Add(message);
            }
            if (count > 0)
            {
                if (id > 0)
                {
                    this.ltlMsg.Text = "操作成功，已修改该信息";
                    Response.Redirect("storemessage_edit.aspx?id=" + id.ToString());
                }
                else
                {
                    this.ltlMsg.Text = "操作成功，已添加该信息";
                    Response.Redirect("storemessage_list.aspx");
                }

                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else
            {
                this.ltlMsg.Text = "操作失败";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }

        }
        #endregion

        protected void btnUploadAccessory1_Click(object sender, EventArgs e)
        {
            UploadFile(fuAccessory1, hlAccessory1, fuAccessory1Tip,hfAccessory1);
        }

        protected void btnUploadAccessory2_Click(object sender, EventArgs e)
        {
            UploadFile(fuAccessory2, hlAccessory2, fuAccessory2Tip,hfAccessory2);
        }
        protected void btnUploadAccessory3_Click(object sender, EventArgs e)
        {
            UploadFile(fuAccessory3, hlAccessory3, fuAccessory3Tip, hfAccessory3);
        }
        protected void btnUploadAccessory4_Click(object sender, EventArgs e)
        {
            UploadFile(fuAccessory4, hlAccessory4, fuAccessory4Tip, hfAccessory4);
        }
        protected void btnUploadAccessory5_Click(object sender, EventArgs e)
        {
            UploadFile(fuAccessory5, hlAccessory5, fuAccessory5Tip, hfAccessory5);
        }
    }
}
